.TH std::abs(float),std::fabs,std::fabsf,std::fabsl 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::abs(float),std::fabs,std::fabsf,std::fabsl \- std::abs(float),std::fabs,std::fabsf,std::fabsl

.SH Synopsis
   Defined in header <cmath>
   Defined in header <cstdlib>
   float       abs( float num );

   double      abs( double num );                        (until C++23)

   long double abs( long double num );
   constexpr /* floating-point-type */
               abs( /* floating-point-type */            (since C++23)
   num );
   Defined in header <cmath>
   float       fabs ( float num );

   double      fabs ( double num );                                    (until C++23)

   long double fabs ( long double num );         \fB(1)\fP
   constexpr /* floating-point-type */
               fabs ( /* floating-point-type */                        (since C++23)
   num );
                                                                       \fI(since C++11)\fP
   float       fabsf( float num );                   \fB(2)\fP \fB(3)\fP           (constexpr since
                                                                       C++23)
                                                                       \fI(since C++11)\fP
   long double fabsl( long double num );                 \fB(4)\fP           (constexpr since
                                                                       C++23)
   Additional overloads \fI(since C++11)\fP
   Defined in header <cmath>
   template< class Integer >                                           \fI(since C++11)\fP
   double      fabs ( Integer num );                     (A)           (constexpr since
                                                                       C++23)

   1-4) Computes the absolute value of the floating-point value num.
   The library provides overloads of std::abs and std::fabs for all cv-unqualified
   floating-point types as the type of the parameter num.
   (since C++23)

   A) Additional overloads are provided for all integer types, which are  \fI(since C++11)\fP
   treated as double.

   For integral arguments, the integral overloads of std::abs are likely better
   matches. If std::abs is called with an unsigned integral argument that cannot be
   converted to int by integral promotion, the program is ill-formed.

.SH Parameters

   num - floating-point or integer value

.SH Return value

   If successful, returns the absolute value of arg (|arg|). The value returned is
   exact and does not depend on any rounding modes.

.SH Error handling

   This function is not subject to any of the error conditions specified in
   math_errhandling.

   If the implementation supports IEEE floating-point arithmetic (IEC 60559),

     * If the argument is ±0, +0 is returned.
     * If the argument is ±∞, +∞ is returned.
     * If the argument is NaN, NaN is returned.

.SH Notes

   The additional overloads are not required to be provided exactly as (A). They only
   need to be sufficient to ensure that for their argument num of integer type,
   std::fabs(num) has the same effect as std::fabs(static_cast<double>(num)).

.SH Example


// Run this code

 #include <cmath>
 #include <iostream>

 int main()
 {
     std::cout << "abs(+3.0) = " << std::abs(+3.0) << '\\n'
               << "abs(-3.0) = " << std::abs(-3.0) << '\\n';

     // special values
     std::cout << "abs(-0.0) = " << std::abs(-0.0) << '\\n'
               << "abs(-Inf) = " << std::abs(-INFINITY) << '\\n'
               << "abs(-NaN) = " << std::abs(-NAN) << '\\n';
 }

.SH Possible output:

 abs(+3.0) = 3
 abs(-3.0) = 3
 abs(-0.0) = 0
 abs(-Inf) = inf
 abs(-NaN) = nan

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to          Behavior as published              Correct behavior
   LWG 2192 C++98      overloads of std::abs were              declared these overloads
                       inconsistently declared in two headers  in both headers
                       overloads of std::abs for integer types
   LWG 2735 C++11      returning double was erroneously        removed the requirement
                       required

.SH See also

   abs(int)
   labs               computes absolute value of an integral value (\\(\\small{|x|}\\)|x|)
   llabs              \fI(function)\fP
   \fI(C++11)\fP
   copysign
   copysignf
   copysignl          copies the sign of a floating point value
   \fI(C++11)\fP            \fI(function)\fP
   \fI(C++11)\fP
   \fI(C++11)\fP
   signbit            checks if the given number is negative
   \fI(C++11)\fP            \fI(function)\fP
   abs(std::complex)  returns the magnitude of a complex number
                      \fI(function template)\fP
   abs(std::valarray) applies the function abs to each element of valarray
                      \fI(function template)\fP
   C documentation for
   fabs
